#include "game_over_state.h"
#include "game.h"
#include "input_handler.h"
#include "main_menu_state.h"
#include "menu_button.h"
#include "play_state.h"
#include "state_parser.h"
#include "texture_manager.h"
#include <iostream>

const std::string GameOverState::s_gameOverID = "GAMEOVER";

void GameOverState::s_gameOverToMain()
{
    TheGame::Instance()->getStateMachine()->changeState(new MainMenuState());
}

void GameOverState::s_restartPlay()
{
    TheGame::Instance()->getStateMachine()->changeState(new PlayState());
}

void GameOverState::update()
{
    if(m_loadingComplete && !m_gameObjects.empty())
    {
        for(unsigned int i = 0; i < m_gameObjects.size(); i++)
        {
            m_gameObjects[i]->update();
        }
    }
}

void GameOverState::render()
{
    if(m_loadingComplete && !m_gameObjects.empty())
    {
        for(unsigned int i = 0; i < m_gameObjects.size(); i++)
        {
            m_gameObjects[i]->draw();
        }
    }
    if (playerWin)
        TheTextureManager::Instance()->drawFrame("victory", 144, 50, 1024, 512, 0, 0, TheGame::Instance()->getRenderer(), 0, 255, 0.5);
    else
        TheTextureManager::Instance()->drawFrame("defeat", 144, 50, 1024, 512, 0, 0, TheGame::Instance()->getRenderer(), 0, 255, 0.5);
}

bool GameOverState::onEnter()
{
    // parse the state
    StateParser stateParser;
    stateParser.parseState("battleships.xml", s_gameOverID, &m_gameObjects, &m_textureIDList);
    
    m_callbacks.push_back(0);
    m_callbacks.push_back(s_gameOverToMain);
    m_callbacks.push_back(s_restartPlay);
    
    // set the callbacks for menu items
    setCallbacks(m_callbacks);

    TheTextureManager::Instance()->load("images/EndOfGame_V.png", "victory", TheGame::Instance()->getRenderer());
    TheTextureManager::Instance()->load("images/EndOfGame_D.png", "defeat", TheGame::Instance()->getRenderer());
    
    m_loadingComplete = true;
    
    std::cout << "entering GameOverState\n";
    return true;
}

bool GameOverState::onExit()
{
    if(m_loadingComplete && !m_gameObjects.empty())
    {
        for(unsigned int i = 0; i < m_gameObjects.size(); i++)
        {
            m_gameObjects[i]->clean();
            //delete m_gameObjects[i];
        }
        
        m_gameObjects.clear();
    }
    
    // clear the texture manager
    for(unsigned int i = 0; i < m_textureIDList.size(); i++)
    {
        TheTextureManager::Instance()->clearFromTextureMap(m_textureIDList[i]);
    }
    
    TheInputHandler::Instance()->reset();
    
    std::cout << "exiting GameOverState\n";
    return true;
}

void GameOverState::setCallbacks(const std::vector<Callback>& callbacks)
{
    // go through the game objects
    for(unsigned int i = 0; i < m_gameObjects.size(); i++)
    {
        // if they are of type MenuButton then assign a callback based on the id passed in from the file
        if(dynamic_cast<MenuButton*>(m_gameObjects[i]))
        {
            MenuButton* pButton = dynamic_cast<MenuButton*>(m_gameObjects[i]);
            pButton->setCallback(callbacks[pButton->getCallbackID()]);
        }
    }
}

GameOverState::GameOverState(bool playerWin) {
    this->playerWin = playerWin;
}
